Tutoial - Part 2 TutoRial - Part 2

Marine Ecosystem Dynamics - 2024

Author

Kinlan M.G. Jan

Pipes

Pipes, expressed as %>% or |>, are very useful and make our code clearer. Using pipes, our data flow from one function to another.

Exercises

  • Rewrite these chunks of code using the pipes
sum(c(2.2,4.1,2,pi))
c(2.2,4.1,2,pi) |> sum()
# OR
c(2.2,4.1,2,pi) %>% sum()
round(sum(c(2.2,4.1,2,pi)))
c(2.2,4.1,2,pi) |> sum() |> round()
# OR
c(2.2,4.1,2,pi) %>% sum() %>% round()
round(sum(c(2.2,4.1,2,pi)), digits = 3)
c(2.2,4.1,2,pi) |> sum() |> round(digits = 3)
# OR
c(2.2,4.1,2,pi) %>% sum() %>% round(digits = 3)

Tidy the data with tidyr

As seen in the slides, a tidy table has:

  1. Each variable in its own column
  2. Each observation in its own row

To reach this, tidyr has 4 key functions:

  1. pivot_longer
  2. pivot_wider
  3. unite
  4. separate

Exercises

  • If this is not done yet, download the dataset zooplankton_seasonality.csv
  • Import the dataset in your environment

  • Is this dataset a tidy dataset?

First 6 rows of the dataset zooplankton_seasonality
Month_abb Year Station Coordinates Group Taxa Biomass
Jan 2009 BY15 20.05000/57.33333 Copepoda Acartia 6.650319
Jan 2009 BY31 18.23333/58.58812 Copepoda Acartia 1.816994
Jan 2009 BY5 15.98333/55.25000 Copepoda Acartia 5.562097
Jan 2009 BY15 20.05000/57.33333 Copepoda Centropages 5.738562
Jan 2009 BY31 18.23333/58.58812 Copepoda Centropages 1.228759
Jan 2009 BY5 15.98333/55.25000 Copepoda Centropages 14.405224

Each variable has its own column
Each variable has its own row
Coordinates has 2 values

  • Separate the column Coordinates in 2 news columns: Longitude and Latitude
First 6 rows of the dataset zooplankton_seasonality with the column Coordinates separated as Longitude and Latitude
Month_abb Year Station Longitude Latitude Group Taxa Biomass
Jan 2009 BY15 20.05000 57.33333 Copepoda Acartia 6.650319
Jan 2009 BY31 18.23333 58.58812 Copepoda Acartia 1.816994
Jan 2009 BY5 15.98333 55.25000 Copepoda Acartia 5.562097
Jan 2009 BY15 20.05000 57.33333 Copepoda Centropages 5.738562
Jan 2009 BY31 18.23333 58.58812 Copepoda Centropages 1.228759
Jan 2009 BY5 15.98333 55.25000 Copepoda Centropages 14.405224
library(tidyr)
zooplankton |> separate(Coordinates, into = c("Longitude", "Latitude"), sep = "/")